package com.lingyu.jsjx.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingyu.jsjx.domain.TeachingReflection;
import com.lingyu.jsjx.service.ITeachingReflectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.lingyu.jsjx.domain.TeacherTenet;
import com.lingyu.jsjx.service.ITeacherTenetService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教学心旨Controller
 *
 * @author ruoyi
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/teacherTenet")
public class TeacherTenetController extends BaseController {
    @Autowired
    private ITeacherTenetService teacherTenetService;

    @Autowired
    private ITeachingReflectionService teachingReflectionService;

    /**
     * 查询教学心旨列表
     */
    @RequiresPermissions("jsjx:teacherTenet:list")
    @GetMapping("/list")
    public TableDataInfo list(TeacherTenet teacherTenet) {
        startPage();
        List<TeacherTenet> list = teacherTenetService.selectTeacherTenetList(teacherTenet);
        return getDataTable(list);
    }

    /**
     * 导出教学心旨列表
     */
    @RequiresPermissions("jsjx:teacherTenet:export")
    @Log(title = "教学心旨", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeacherTenet teacherTenet) {
        List<TeacherTenet> list = teacherTenetService.selectTeacherTenetList(teacherTenet);
        ExcelUtil<TeacherTenet> util = new ExcelUtil<TeacherTenet>(TeacherTenet.class);
        util.exportExcel(response, list, "教学心旨数据");
    }

    /**
     * 获取教学心旨详细信息
     */
    @RequiresPermissions("jsjx:teacherTenet:query")
    @GetMapping(value = "/{tenetId}")
    public AjaxResult getInfo(@PathVariable("tenetId") Long tenetId) {
        return success(teacherTenetService.selectTeacherTenetByTenetId(tenetId));
    }

    /**
     * 新增教学心旨
     */
    @RequiresPermissions("jsjx:teacherTenet:add")
    @Log(title = "教学心旨", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeacherTenet teacherTenet) {
        return toAjax(teacherTenetService.insertTeacherTenet(teacherTenet));
    }

    /**
     * 修改教学心旨
     */
    @RequiresPermissions("jsjx:teacherTenet:edit")
    @Log(title = "教学心旨", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeacherTenet teacherTenet) {
        return toAjax(teacherTenetService.updateTeacherTenet(teacherTenet));
    }

    /**
     * 删除教学心旨
     */
    @RequiresPermissions("jsjx:teacherTenet:remove")
    @Log(title = "教学心旨", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tenetIds}")
    public AjaxResult remove(@PathVariable Long[] tenetIds) {
        return toAjax(teacherTenetService.deleteTeacherTenetByTenetIds(tenetIds));
    }

    @RequiresPermissions("jsjx:teacherTenet:query")
    @GetMapping(value = "/getRecentTeacherReflection")
    public AjaxResult getRecentTeacherReflection(@RequestParam("teacherTenetId") Long teacherTenetId) {
        TeacherTenet teacherTenet = teacherTenetService.selectTeacherTenetByTenetId(teacherTenetId);
        Date updateTime = teacherTenet.getUpdateTime();
        QueryWrapper<TeachingReflection> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("teach_time", updateTime);
        List<TeachingReflection> list = teachingReflectionService.list(queryWrapper);
        TeachingReflection teachingReflection = new TeachingReflection();
        if(!list.isEmpty()){
            teachingReflection = list.stream()
                    .skip(list.size() - 1)
                    .findFirst()
                    .orElse(null);
        }
        return success(teachingReflection);
    }

    @Log(title = "教学心旨", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TeacherTenet> util = new ExcelUtil<>(TeacherTenet.class);
        List<TeacherTenet> teacherTenetList = util.importExcel(file.getInputStream());
        String message = teacherTenetService.importTeacherTenet(teacherTenetList, false);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TeacherTenet> util = new ExcelUtil<>(TeacherTenet.class);
        util.importTemplateExcel(response, "教学心旨数据");
    }
}
